package com.governtalk.backend.controller;

import com.governtalk.backend.dto.user.UserProfileDto;
import com.governtalk.backend.dto.question.QuestionDto;
import com.governtalk.backend.dto.forum.PostDto;
import com.governtalk.backend.service.UserService;
import com.governtalk.backend.service.ResourceService;
import com.governtalk.backend.service.QuestionService;
import com.governtalk.backend.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private ResourceService resourceService;
    
    @Autowired
    private QuestionService questionService;
    
    @Autowired
    private ForumService forumService;
    
    // 用户管理
    @GetMapping("/users")
    public ResponseEntity<Page<UserProfileDto>> getUserList(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String role,
            @RequestParam(required = false) String status,
            Pageable pageable) {
        Page<UserProfileDto> users = userService.getUserList(keyword, role, status, pageable);
        return ResponseEntity.ok(users);
    }
    
    @PutMapping("/users/{userId}/status")
    public ResponseEntity<String> updateUserStatus(
            @PathVariable Long userId,
            @RequestBody Map<String, String> statusData) {
        String status = statusData.get("status");
        userService.updateUserStatus(userId, status);
        return ResponseEntity.ok("User status updated successfully");
    }
    
    @PutMapping("/users/{userId}/role")
    public ResponseEntity<String> updateUserRole(
            @PathVariable Long userId,
            @RequestBody Map<String, String> roleData) {
        String role = roleData.get("role");
        userService.updateUserRole(userId, role);
        return ResponseEntity.ok("User role updated successfully");
    }
    
    // 资源管理
    @PutMapping("/resources/{resourceId}/approval")
    public ResponseEntity<String> approveResource(
            @PathVariable Long resourceId,
            @RequestBody Map<String, Object> approvalData) {
        Boolean approved = (Boolean) approvalData.get("approved");
        String reason = (String) approvalData.get("reason");
        resourceService.approveResource(resourceId, approved, reason);
        return ResponseEntity.ok("Resource approval updated successfully");
    }
    
    @DeleteMapping("/resources/{resourceId}")
    public ResponseEntity<String> deleteResource(@PathVariable Long resourceId) {
        resourceService.deleteResource(resourceId);
        return ResponseEntity.ok("Resource deleted successfully");
    }
    
    // 题目管理
    @PostMapping("/questions")
    public ResponseEntity<QuestionDto> createQuestion(@RequestBody QuestionDto questionDto) {
        QuestionDto question = questionService.createQuestion(questionDto);
        return ResponseEntity.ok(question);
    }
    
    @PutMapping("/questions/{questionId}")
    public ResponseEntity<QuestionDto> updateQuestion(
            @PathVariable Long questionId,
            @RequestBody QuestionDto questionDto) {
        QuestionDto question = questionService.updateQuestion(questionId, questionDto);
        return ResponseEntity.ok(question);
    }
    
    @DeleteMapping("/questions/{questionId}")
    public ResponseEntity<String> deleteQuestion(@PathVariable Long questionId) {
        questionService.deleteQuestion(questionId);
        return ResponseEntity.ok("Question deleted successfully");
    }
    
    @PostMapping("/questions/import")
    public ResponseEntity<Map<String, Object>> importQuestions(@RequestParam("file") org.springframework.web.multipart.MultipartFile file) {
        Map<String, Object> result = questionService.importQuestions(file);
        return ResponseEntity.ok(result);
    }
    
    // 论坛管理
    @PutMapping("/forum/posts/{postId}/status")
    public ResponseEntity<String> approvePost(
            @PathVariable Long postId,
            @RequestBody Map<String, String> statusData) {
        String status = statusData.get("status");
        forumService.approvePost(postId, status);
        return ResponseEntity.ok("Post status updated successfully");
    }
    
    @GetMapping("/forum/posts/pending")
    public ResponseEntity<Page<PostDto>> getPendingPosts(Pageable pageable) {
        Page<PostDto> posts = forumService.getPendingPosts(pageable);
        return ResponseEntity.ok(posts);
    }
}
